<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>课程管理</title>
</head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="../css/student.css">
<link rel="stylesheet" href="../css/header.css">

<body style="background: url('../img/bg-min.jpg');background-size: cover;">
    <div id="app5">
        <div class="header">
            <div class="logo">
                <h1>学生课程管理系统</h1>
            </div>
            <el-menu class="el-menu-nav" mode="horizontal" background-color="#545c64" text-color="#fff"
                active-text-color="#ffd04b" router>
                <el-submenu index="1">
                    <template slot="title">个人信息</template>
                    <el-menu-item @click="toMember">{{userName}}</el-menu-item>
                </el-submenu>
            </el-menu>
            <div class="line"></div>
        </div>
        <div class="main">
            <div class="leftMenu" style="width: 145px;">
                <el-row class="tac" style="width: 145px;">
                    <el-menu default-active="3" class="el-menu-vertical-demo" background-color="#545c64"
                        text-color="#fff" active-text-color="#ffd04b">
                        <el-menu-item index="1" @click="toStudent">
                            <span slot="title">学生管理</span>
                        </el-menu-item>
                        <el-menu-item index="2" v-show="adminFlag" @click="toTeacher">
                            <span slot="title">老师管理</span>
                        </el-menu-item>
                        <el-menu-item index="3" @click="toSubject">
                            <span slot="title">课程管理</span>
                        </el-menu-item>
                        <el-menu-item index="4" @click="toTeacherInfo">
                            <span slot="title">个人信息</span>
                        </el-menu-item>
                    </el-menu>
                </el-row>
            </div>
            <div class="rightMenu subject-state">
                <div>
                    <el-button type="primary" class="addStudentBtn" @click="addSubject">新增</el-button>
                </div>
                <el-table :data="tableData" style="width: 100%">
                    <el-table-column prop="name" label="课程名称">
                    </el-table-column>
                    <el-table-column prop="teacher_name" label="老师">
                    </el-table-column>
                    <el-table-column prop="start_date" label="开始时间">
                    </el-table-column>
                    <el-table-column prop="end_date" label="结束时间">
                    </el-table-column>
                    <el-table-column prop="status" label="审核状态">
                    </el-table-column>
                    <el-table-column prop="desc" label="备注">
                    </el-table-column>
                    <el-table-column label="操作" width="450">
                        <template slot-scope="scope">
                            <el-button size="mini" type="danger" @click="deleteSubject(scope.$index, scope.row)">删除
                            </el-button>
                            <el-button size="mini" type="warning" @click="detailSubject(scope.$index, scope.row)">详情
                            </el-button>
                            <el-button size="mini" type="success" v-show="adminFlag" @click="reviewSubject(scope.$index, scope.row,1)">通过
                            </el-button>
                            <el-button size="mini" type="primary" @click="releaseSubject(scope.$index, scope.row,2)">发布
                            </el-button>
                            <el-button size="mini" type="warning" v-show="adminFlag" @click="reviewSubject(scope.$index, scope.row,3)">不通过
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!--添加课程信息-->
            <el-dialog title="添加课程信息" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="课程名称" :label-width="formLabelWidth">
                        <el-input v-model="form.name" placeholder="课程名称" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="老师" :label-width="formLabelWidth">
                        <el-input v-model="form.teacher_name" placeholder="老师姓名" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="开始时间" :label-width="formLabelWidth">
                        <el-date-picker v-model="form.start_date" value-format="yyyy-MM-dd" type="date"
                            placeholder="选择开始时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结束时间" :label-width="formLabelWidth">
                        <el-date-picker v-model="form.end_date" value-format="yyyy-MM-dd" type="date"
                            placeholder="选择结束时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth">
                        <el-input v-model="form.desc" placeholder="备注" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirAdd">确 定</el-button>
                </div>
            </el-dialog>
            <!--课程详情弹窗-->
            <el-dialog title="课程详情" :visible.sync="subjectDetailDataFlag">
                <el-button type="primary" @click="addSubjectDetail">新增</el-button>
                <el-table :data="subjectDetailData">
                    <el-table-column property="subject_name" label="课程名称"></el-table-column>
                    <el-table-column property="date" label="课程日期"></el-table-column>
                    <el-table-column property="time_start" label="开始时间"></el-table-column>
                    <el-table-column property="time_end" label="结束时间"></el-table-column>
                    <el-table-column property="teacher_name" label="任课老师"></el-table-column>
                </el-table>
            </el-dialog>
            <!--添加课程详情信息-->
            <el-dialog title="添加课程信息" :visible.sync="dialogFormDetailVisible">
                <el-form :model="formDetail">
                    <el-form-item label="课程日期" :label-width="formLabelWidth">
                        <el-date-picker v-model="formDetail.date" value-format="yyyy-MM-dd" type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="课程时间" :label-width="formLabelWidth">
                        <el-select v-model="formDetail.time_id" placeholder="请选择">
                            <el-option v-for="item in coursetimeList" :key="item.id" :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormDetailVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirmAddDetail">确 定</el-button>
                </div>
            </el-dialog>

        </div>
    </div>

</body>
<script>
    var app5 = new Vue({
        el: '#app5',
        data: {
            subject: {},
            index: null,
            userName: "",
            adminFlag: false,
            url: 'http://127.0.0.1:8080',
            userName: "",
            adminFlag: false,
            dialogFormVisible: false,
            dialogFormEditVisible: false,
            tableData: [],
            form: {
                name: '',
                teacher_name: '',
                start_date: '',
                end_date: '',
                desc: ''
            },
            formEdit: {
                name: '',
                sex: '',
                mobile: ''
            },
            formDetail: {
                id: null,
                date: '',
                subject_id: '',
                subject_name: '',
                teacher_id: null,
                teacher_name: '',
                time_id: ''
            },
            subjectDetailData: [],
            coursetimeList: [],
            subjectDetailDataFlag: false,
            dialogFormDetailVisible: false,
            formLabelWidth: '120px'
        },
        mounted: function () {
            if (window.localStorage.getItem("userName") == "admin") {
                this.adminFlag = true;
            }
            this.getSubjectList()
            this.userName = window.localStorage.getItem("userName")
        },
        methods: {
            //学生管理
            toStudent: function () {
                window.location.href = "studentManage.html"
            },
            toTeacher: function () { window.location.href = "teacherManage.html" },
            toSubject: function () { window.location.href = "subjectManage.html" },
            toTeacherInfo: function () { window.location.href = "teacherInfo.html" },
            toMember: function () {
                window.location.href = "teacherInfo.html"
            },
            //查询课程列表
            getSubjectList: function () {
                var _this = this;
                let currentData = [];
                var queryUrl;
                if (_this.adminFlag) {
                    queryUrl = this.url + '/subject/queryAll?status=0';
                }else {
                    queryUrl = this.url + '/subject/query?creator='+ window.localStorage.getItem("creator");
                }

                axios.get(queryUrl, {
                    params: {}
                }).then(function (res) {
                    if (res.data.msg == 'success') {
                        res = res.data;
                        _this.tableData = res.obj.map((item, index) => {
                            if (item.status == 0) {
                                item.status = '待审核'
                            }
                            if (item.status == 1) {
                                item.status = '审核通过'
                            }
                            if (item.status == 2) {
                                item.status = '发布'
                            }
                            if (item.status == 3) {
                                item.status = '不通过'
                            }
                            return item
                        })
                    }

                })
            },
            //打开新增窗口
            addSubject: function () {
                this.dialogFormVisible = true
            },
            //确认新增
            confirAdd: function () {
                var _this = this;
                //                this.judgeRule("add");
                axios.post(this.url + '/subject/insert', {
                    teacher_id: parseInt(window.localStorage.getItem("creator")),
                    name: this.form.name,
                    teacher_name: this.form.teacher_name,
                    start_date: this.form.start_date,
                    end_date: this.form.end_date,
                    desc: this.form.desc,
                    status: "0",
                    creator: parseInt(window.localStorage.getItem("creator"))
                }).then(function (res) {
                    if (res.data.msg == 'success') {
                        _this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        _this.getSubjectList()
                    } else {
                        _this.$message.error('添加失败');
                    }
                });
                this.dialogFormVisible = false;
            },
            //删除
            deleteSubject: function (index, row) {
                var _this = this;
                this.$confirm('是否确定删除此信息', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(this.url + '/subject/delete?id=' + row.id).then(function (res) {
                        if (res.data.msg == 'success') {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getSubjectList()
                        } else {
                            _this.$message.error('删除失败');
                        }
                    })
                }).catch(() => {
                    _this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //发布
            releaseSubject: function (index, row, status) {
                if (row.status != "审核通过"){
                    this.$message({
                        message: '未审核,无法发布',
                        type: 'success'
                    });
                    return
                }
                var _this = this;
                this.$confirm('是否确定发布此课程', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(this.url + '/subject/update?id=' + row.id + "&status=" + status).then(function (res) {
                        if (res.data.msg == 'success') {
                            res = res.data;
                            _this.$message({
                                message: '发布成功',
                                type: 'success'
                            });
                            _this.getSubjectList()
                        } else {
                            _this.$message.error('发布失败');
                        }
                    })
                }).catch(() => {
                    _this.$message({
                        type: 'info',
                        message: '已取发布'
                    });
                });

            },
            //审核
            reviewSubject: function (index, row, status) {
                var _this = this;
                axios.get(this.url + '/subject/update?id=' + row.id + "&status=" + status).then(function (res) {
                    if (res.data.msg == 'success') {
                        res = res.data;
                        _this.$message({
                            message: '完成',
                            type: 'success'
                        });
                        _this.getSubjectList()
                    } else {
                        _this.$message.error('失败');
                    }
                })
            },
            //查询课程详情
            detailSubject: function (index, row) {
                var _this = this;
                this.index = index;
                this.subject = row;
                this.subjectDetailDataFlag = true
                axios.get(this.url + '/subjectdetail/query', {
                    params: {
                        subject_id: row.id
                    }
                }).then(function (res) {
                    if (res.data.msg == 'success') {
                        res = res.data;
                        _this.subjectDetailData = res.obj
                    }

                })
            },
            //添加课程详情
            addSubjectDetail: function () {
                var _this = this
                this.coursetimeList = this.coursetimeList.map((item, v) => {
                    item.name = item.name + "-" + item.start_time + "-" + item.end_time + ""
                    return item
                })
                //查询时间
                axios.get(this.url + '/coursetime/query', {}).then(function (res) {
                    if (res.data.msg == 'success') {
                        res = res.data;
                        _this.coursetimeList = res.obj
                    }
                })
                this.dialogFormDetailVisible = true
            },
            confirmAddDetail: function () {
                var _this = this;
                axios.post(this.url + '/subjectdetail/insert', {
                    subject_id: this.subject.id.toString(),
                    subject_name: this.subject.name,
                    time_id: this.formDetail.time_id.toString(),
                    teacher_id: parseInt(window.localStorage.getItem("creator")),
                    teacher_name: this.subject.teacher_name,
                    date: this.formDetail.date
                }).then(function (res) {
                    if (res.data.msg == 'success') {
                        _this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        _this.detailSubject(_this.index, _this.subject)
                    } else {
                        _this.$message.error('添加失败');
                    }
                });
                this.dialogFormDetailVisible = false;
            },
            //字段校验
            judgeRule: function (item) {
                let currentItem = {}
                if (item == "add") {
                    currentItem = this.form
                } else {
                    currentItem = this.formEdit
                }

                if (!this.currentItem.name) return this.$message({
                    message: '请输入课程名称',
                    type: 'warning'
                })
                if (!this.currentItem.teacher_name) return this.$message({
                    message: '请输入老师姓名',
                    type: 'warning'
                })
                if (!this.currentItem.start_date) return this.$message({
                    message: '请输入开始时间',
                    type: 'warning'
                })
                if (!this.currentItem.end_date) return this.$message({
                    message: '请输入结束时间',
                    type: 'warning'
                })
            }
        }
    });
</script>

</html>
